var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHei: 0,
    curSwiperIndex: 0,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    loadingHidden: false
  },
  onShareAppMessage: function() {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },
  onLoad: function() {
    var _self = this;

    _self._initApp(function() {
      _self._getPageData();
      _self._quietLogin();
    });
  },

  _initApp(cb) {
    wx.getExtConfig && wx.getExtConfig({
      success: function(res) {
        var {
          host,
          alias
        } = res.extConfig;

        if (host) {
          app.setHost(host);
        }
        if (alias) {
          app.setAlias(alias);
        }
        cb && cb();
      }
    });
  },

  // onPullDownRefresh: function () {
  //   this._getPageData();
  //   this._quietLogin();
  // },
  _toMap() {
    var shop = this.data.shop;

    ajax.G('/openapi/CAddress/getLocation', {
      addr: shop.address
    }, function(data) {
      var addressInfo = JSON.parse(data.result)['result'];

      wx.openLocation({
        latitude: addressInfo.location.lat,
        longitude: addressInfo.location.lng,
        name: shop.name,
        address: shop.address,
      })
    })
  },

  /**
   * openid静默登陆
   */
  _quietLogin: function() {
    var $this = this;

    wx.login({
      success: function(res) {
        ajax.G(
          '/openapi/CHome/getUserOpenID', {
            jscode: res.code
          },
          function(d) {
            app.setOpenId(d.result.openid);
            $this._openIdLogin(d.result.openid);
          }
        );
      },
      fail: function() {},
      complete: function() {}
    })
  },

  /**
   * openid静默登陆
   */
  _openIdLogin: function(openid) {
    var $this = this;
    ajax.P(
      '/openapi/CHome/login', {
        openId: openid
      },
      function(d) {
        var userSid = d.result.userSid;
        if (userSid) {
          app.setUserSid(userSid);
        }
      }
    );
  },
  /**
   * 根据图片高度设置swiper高度
   */
  _swiperImgLoaded: function(evt) {
    if (this.data.swiperHei > 0) {
      return;
    }
    var self = this;
    var ratio = evt.detail.height / evt.detail.width;

    wx.getSystemInfo({
      success: function(res) {
        self.setData({
          dataLoaded: true,
          swiperHei: res.windowWidth * ratio
        });
      }
    });
  },

  //获取页面数据
  _getPageData: function() {
    var _self = this;

    ajax.P('/openapi/CTechnician/index', {}, function(result) {
      var d = result.result;

      _self.setData({
        shop: {
          name: d.title,
          phone: d.contact,
          address: d.address,
          time: d.hours,
          logo: d.logo,
          shareTitle: d.share_title,
          kfState: d.kf_state
        },
        bannerList: d.bannerList,
        homeCatSetting: d.homeCatSetting,
        goodsModuleList: d.goodsModuleList,
        techList: d.technician,
        dataLoaded: true,
      });
      console.log(_self.data.bannerList);
      app.setShop(_self.data.shop);
    })
  },

})